jQuery(document).ready(function() {
    var csrf_hash = jQuery('#ifree_token').val();
    jQuery.ajaxSetup({
        data: {
            csrf_ifree: csrf_hash
        }
    });

    jQuery('.stdtablecb .checkall').click(function() {
        var parentTable = jQuery(this).parents('table');
        var ch = parentTable.find('tbody input[type=checkbox]');
        if (jQuery(this).is(':checked')) {

            //check all rows in table
            ch.each(function() {
                jQuery(this).attr('checked', true);
                jQuery(this).parent().addClass('checked');	//used for the custom checkbox style
                jQuery(this).parents('tr').addClass('selected');
            });

            //check both table header and footer
            parentTable.find('.checkall').each(function() {
                jQuery(this).attr('checked', true);
            });

        } else {

            //uncheck all rows in table
            ch.each(function() {
                jQuery(this).attr('checked', false);
                jQuery(this).parent().removeClass('checked');	//used for the custom checkbox style
                jQuery(this).parents('tr').removeClass('selected');
            });

            //uncheck both table header and footer
            parentTable.find('.checkall').each(function() {
                jQuery(this).attr('checked', false);
            });
        }
    });


    jQuery('.stdtablecb tbody input[type=checkbox]').click(function() {
        if (jQuery(this).is(':checked')) {
            jQuery(this).parents('tr').addClass('selected');
        } else {
            jQuery(this).parents('tr').removeClass('selected');
        }
    });

    //delete selected row in table
    jQuery('.deletebutton').click(function() {
        var c = confirm('Continue delete?');
        if (c) {
            var tb = jQuery(this).attr('title');							// get target id of table								   
            var sel = false;											//initialize to false as no selected row
            var ch = jQuery('#' + tb).find('tbody input[type=checkbox]');		//get each checkbox in a table
            var url = jQuery(this).attr('value');

            //check if there is/are selected row in table
            ch.each(function() {
                if (jQuery(this).is(':checked')) {
                    sel = true;
                    id = jQuery(this).attr('value');
                    jQuery.post(url, {id: id}, function(data) {

                    });//set to true if there is/are selected row
                    jQuery(this).parents('tr').fadeOut(function() {
                        jQuery(this).remove();							//remove row when animation is finished
                    });
                }
            });

            if (!sel)
                alert('No data selected');							//alert to no data selected
        }
    });


    //delete individual row
    jQuery('.stdtable a.delete').click(function() {
        var c = confirm('Continue delete?');
        if (c) {
            var url = jQuery(this).attr('href');
            var id = jQuery(this).attr('data');
            jQuery.post(url, {id: id}, function(data) {
            });

            jQuery(this).parents('tr').fadeOut(function() {
                jQuery(this).remove();
            });
        }
        return false;
    });

   
});